package sdu.icat.pj.shiro.configuration;

import javax.annotation.Resource;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import sdu.icat.pj.shiro.Realm;
import sdu.icat.pj.shiro.RedisSessionDao;


/**
 * @author ljh_2015
 * shiro 配置类
 *
 */
@Configuration
public class ShiroConfiguration {

    private final Long outTime = 1800*1000L;
    private final Long validateTime = 300*1000L;
    
    @Bean
    public RedisSessionDao sessionDao() {
	return new RedisSessionDao();
    }
    
    @Bean
    public Realm realm() {
	return new Realm();
    }
    
    @Bean
    public SimpleCookie cookies() {
	SimpleCookie cookies = new SimpleCookie("JSID");
	cookies.setHttpOnly(true);
	cookies.setMaxAge(-1);
	return cookies;
    }
    
    @Bean
    public RedisCacheManager redisCacheManager() {
	return new RedisCacheManager();
    }
   
    @Bean
    public SessionManager configWebSessionManager() {
	DefaultWebSessionManager manager = new DefaultWebSessionManager();  
	manager.setSessionDAO(sessionDao());// 设置SessionDao  
	manager.setSessionIdCookieEnabled(true);
	manager.setSessionIdCookie(cookies());
	manager.setDeleteInvalidSessions(true);// 删除过期的session  
	manager.setGlobalSessionTimeout(outTime);// 设置全局session超时时间  
	manager.setSessionValidationInterval(validateTime);
	manager.setSessionValidationSchedulerEnabled(true);
	manager.setCacheManager(redisCacheManager());
	return manager;  
    }
    
    @Bean
    public DefaultWebSecurityManager securityManager() {
	DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
	manager.setRealm(realm());
	manager.setSessionManager(configWebSessionManager());
	//manager.setRememberMeManager(rememberMeManager());
	return manager;
    }
    

    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
	ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
	factoryBean.setSecurityManager(securityManager);
	factoryBean.setLoginUrl("/unauthorized?url=");
	factoryBean.setUnauthorizedUrl("/forbidden");
	factoryBean.setSuccessUrl("/home");
	//设置filterMap
	return factoryBean;
    }

    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
	return new LifecycleBeanPostProcessor();
    }

    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
	DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
	creator.setProxyTargetClass(true);
	return creator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAdvisor() {
	AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
	advisor.setSecurityManager(securityManager());
	return advisor;
    }
}
